package Control;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;

import objects.Gene;

import View.VisualizationPanel;
import View.VisualizationPropertiesPanel;

/**
 * 
 * @author stephenbwilliamson
 */
public class VisualizationSettings implements ActionListener, ItemListener {

	private DataFetcher df = DataFetcher.getInstance();
	private Controller control = Controller.getInstance();

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("comboBoxChanged")) {
			// The actual gene has changed
			JComboBox jcb = (JComboBox) e.getSource();
			String geneName = (jcb.getSelectedIndex() == 0) ? "" : (String) jcb
					.getSelectedItem();
			try {
				Gene gene = InitializeData.getInstance().loadGene(geneName);
				control.setCurrentGene(gene);
				control.clearTreatments();

				df.loadTranscriptInformation(gene);
				df.loadTreatments(gene);
				Zoomable.getInstance().resetZoom();

			} catch (Exception exception) {
				df.handleException(exception);
			}
			VisualizationPropertiesPanel.getInstance().loadTreatments();
		} else if (e.getActionCommand().equals("Display All Subsets")) {
			Controller.getInstance().showAllSubsets();

		} else if (e.getActionCommand().equals("Collapse Subsets")) {
			Controller.getInstance().collapseSubsets();
		} else {
			System.err.println("Action Command: " + e.getActionCommand());
			System.out.println("Event: " + e);
			System.out.println("Param String: " + e.paramString());
		}
		VisualizationPanel.getInstance().repaint();
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		JCheckBox cb = (JCheckBox) e.getItem();
		String text = cb.getText();
		text = text.replaceAll("Treatment ", "");
		int treatmentID = Integer.parseInt(text);
		if (ItemEvent.SELECTED == e.getStateChange()) {
			control.setTreatment(treatmentID);
			df.loadAlignmentInformation(control.getCurrentGene(), treatmentID);
		} else {
			control.removeTreatment(treatmentID);
		}
		VisualizationPanel.getInstance().repaint();
	}
}
